#include "swagme.h"
#include "Swag.h"
#include "Sprite.h"
#include "Game.h"
#include <algorithm>

const int Swag::FrameCount = 2;

// SWAG is hard-coded 12x12
Swag::Swag(Sprite* ps, int x, int y, sm_swag_type swagType, int _pts)
  : TouchableObject(x, y, 12, 12), FrameCounter(0),
    mpSprite(ps), mSwagType(swagType), mPoints(_pts) 
{
  if(!mpSprite) { cerr << "Swag initialized with NULL sprite" << endl; exit(500); }
  FrameCounter = random(0,32*mpSprite->getNumFrames());
    this->sObjName = "Swag";
}

/* // TODO: SHOULD WE JUST GET RID OF THESE ?
Swag::Swag(Sprite* ps, int _pts)
: FrameCounter(0),
  mpSprite(ps), posX(0), posY(0), mPoints(_pts) {
  if(!mpSprite) { cerr << "Swag initialized with NULL sprite" << endl; exit(500); }
  FrameCounter = random(0,32*mpSprite->getNumFrames());
}

Swag::Swag(int _pts)
: FrameCounter(0),
  mpSprite(NULL), posX(0), posY(0), mPoints(_pts) {
  cout << "Swag::Swag(pts) used!" << endl;
}
*/

int Swag::AddEffect(const string &name) {
  Effect *effect = mpSprite->getEffect(name);
  if (effect) {
    mActiveEffects.push_back(name);
    effect->setFrameCounter(0);
  } else {
    cout << "Error: Attempt to add an effect " << name << " which has not been loaded." << endl;
  }
  return 0;
}

int Swag::RemoveEffect(const string &name) {
  ActiveEffectIter aeIt;
  while( (aeIt = find(mActiveEffects.begin(), mActiveEffects.end(), name)) != mActiveEffects.end() ) {
    mActiveEffects.erase(aeIt);
  }
  return 0;
}

int Swag::Draw(SDL_Surface *dst, bool bShadow) {
  int frameNum ;

  if (++FrameCounter >= 32*mpSprite->getNumFrames()) FrameCounter = 0;
  frameNum = FrameCounter /32; // TODO: this is an ugly hack, need a proper central animation timer

  // cout << "x " << posX << " y " << posY << endl;
  return mpSprite->Draw(0, frameNum,dst, (int)getLeft()+MAP_POS_X, (int)getTop()+MAP_POS_Y);
}

Swag::~Swag() {
}

void Swag::Update(Game* pGame, Uint32 prevTick, Uint32 thisTick) {
}
